package com.iwith.dongmoon.listadpater;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.iwith.dongmoon.activity.MemberDetailActivity;
import com.iwith.dongmoon.activity.R;
import com.iwith.dongmoon.gsonlist.MemberListItem;
import com.iwith.dongmoon.util.ImageDownloader;
import com.iwith.dongmoon.util.ImageDownloader.Mode;
import com.iwith.dongmoon.util.SessionManager;

public class FindListAdapter extends ArrayAdapter<MemberListItem>{
	private Context mContext;
	private ArrayList<MemberListItem> items;
	private ImageDownloader mImageDownloader;
	private LayoutInflater mInflater;
	private SessionManager mSessionManager;

	public FindListAdapter(Context context, int textViewResourceId,
			ArrayList<MemberListItem> list) {
		super(context, textViewResourceId, list);
		// TODO Auto-generated constructor stub

		this.mContext = context;
		this.items = list;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mImageDownloader = new ImageDownloader();
		this.mImageDownloader.setMode(Mode.CORRECT);
		this.mSessionManager = new SessionManager(mContext);
	}
	
	class ViewHolder {
		ImageView photo;
		ImageView btn_call;
		TextView name;
		TextView graduation;
		TextView bizType;
		TextView bizName;
	}

	@Override
	public MemberListItem getItem(int position) {
		if(items==null)		return null;
		else				return items.get(position);
	}
	
	@Override
	public int getCount(){
		if(items==null)		return 0;
		else				return super.getCount();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder=new ViewHolder();
			convertView = mInflater.inflate(R.layout.listitem_findlist, null);
			holder.photo = (ImageView)convertView.findViewById(R.id.listitemfindlist_photo);
			holder.btn_call = (ImageView)convertView.findViewById(R.id.listitemfindlist_call);
			holder.name = (TextView) convertView.findViewById(R.id.listitemfindlist_name);
			holder.graduation = (TextView) convertView.findViewById(R.id.listitemfindlist_graduation);
			holder.bizType=(TextView) convertView.findViewById(R.id.listitemfindlist_biztype);
			holder.bizName=(TextView) convertView.findViewById(R.id.listitemfindlist_bizname);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final MemberListItem item = items.get(position);

		if (item != null) {
			if(item.getThumbnail()!=null){
				mImageDownloader.download(item.getThumbnail(), holder.photo);
			}
			holder.name.setText(item.getUsername());
			holder.graduation.setText(item.getGraduation().toString());
			holder.bizType.setText(item.getBizType());
			holder.bizName.setText(item.getBizName());
			convertView.setBackgroundColor((position & 1) == 1 ? 0xffececec : Color.WHITE);
		}

		holder.btn_call.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				int type = 0;
				String[] items = null;
				if(isEmpty(item.getPhone()) == true && isEmpty(item.getBizPhone()) == true)
				{	
					String[] temp = { "전화번호가 없습니다" };
					items = temp;
					type = 0;
				}
				else if(isEmpty(item.getPhone()) == false && isEmpty(item.getBizPhone()) == true)
				{
					String[] temp = { "휴대폰", "취소" };
					items = temp;
					type = 1;
				}
				else if(isEmpty(item.getPhone()) == true && isEmpty(item.getBizPhone()) == false)
				{
					String[] temp = { "업체", "취소" };
					items = temp;
					type = 2;
				}
				else if(isEmpty(item.getPhone()) == false && isEmpty(item.getBizPhone()) == false)
				{
					String[] temp = { "휴대폰", "업체", "취소" };
					items = temp;
					type = 3;
				}
					
				final int finalType = type;
				new AlertDialog.Builder(mContext).setTitle("전화걸기")
				.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
						if(finalType == 0);
						else if(finalType == 1)
						{
							String number = item.getPhone();
							if(which == 0)
								mContext.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number)));
						}
						else if(finalType == 2)
						{
							String number = item.getBizPhone();
							if(which == 0)
								mContext.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number)));
						}
						else if(finalType == 3)
						{
							String number = null;
							
							if(which == 0)
								number = item.getPhone();
							else if(which == 1)
								number = item.getBizPhone();
							
							if(number != null && number.length() > 0)
								mContext.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number)));
						}
																	
						dialog.dismiss();
					}
				})
				.show();
			}
		});
		
		convertView.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, MemberDetailActivity.class);
				String favoriteid = item.getUserid();
				String userid = mSessionManager.getUserid();
				intent.putExtra("userid", userid);
				intent.putExtra("favoriteid", favoriteid);
				mContext.startActivity(intent);
			}
			
		});
		return convertView;
	}
	
	private boolean isEmpty(String content)
	{
		if(content == null)
			return true;
		if(content.length() == 0)
			return true;
		return false;
	}
}
